/**
 * Created by dale on 2017/8/11.
 */
var list;
var staffList;
$(document).ready(function () {

    list= $('#List');
    $('.xh-datagrid-toolbar').parseToolbar();
    var iCUId=addressUri.getParam('id');
    var openType = addressUri.getParam('openType');
    if(openType == 'double'){
        list.parent().next().remove();
        list.parent().remove();
        staffList=$('#staffList');
        staffList.initDataGrid(baseUrl + 'customer/getStaff/'+iCUId,'',new DataGridEvent());
    }else {
        list.initDataGrid(baseUrl + 'customer/getAuth?id='+iCUId,'',new DataGridEvent());
        staffList=$('#staffList');
        staffList.initDataGrid(baseUrl + 'customer/getStaff/'+iCUId,'',new DataGridEvent());
    }



    $('#bt_list_add').click(function(){
        var userIds=staffList.getDataGridSelectedFields('iUserId');
        if(staffList.getDataGridSelectedFields('iUserId').length>0){

            xhExtra.ajaxRequest('customer/saveAuth',{userIds:userIds,id:iCUId},{},function (res) {
                window.top.xhTop.showSuccess(res.msg);
                list.datagrid('reload');
                staffList.datagrid('reload');
            })

        }else {
            window.top.swal('错误,请选择需要添加权限的职工','','error');
            return false;
        }
    });

    $('#bt_modal_confirm',parent.document).on('click',function () {
        var userIds=staffList.getDataGridSelectedFields('iUserId');
        if(staffList.getDataGridSelectedFields('iUserId').length>0){
            xhExtra.ajaxRequest('customer/saveAuth',{userIds:userIds,id:iCUId},{},function (res) {
                window.top.xhTop.showSuccess(res.msg);
                list.datagrid('reload');
                staffList.datagrid('reload');
                window.top.xhTop.closeModal();
            });
        }else {
            window.top.swal('错误,请选择需要添加权限的职工','','error');
            return false;
        }
    });

    $('#bt_list_search').click(function () {
       staffList.addDataGridParams({key:xhExtra.getValue($('#list_search'))});
    });

    $('#bt_list_del').click(function () {
        var ids = list.getDataGridSelectedFields('iCUAId');
        if(list.getDataGridSelectedFields('iCUAId').length>0) {

            xhExtra.ajaxRequest('customer/delAuth',{ids: ids},{},function (res) {
                list.datagrid('reload');
                staffList.datagrid('reload');
            })

        }else {
            window.top.swal('错误,请选择需要移除权限的职工','','error');
            return false;
        }
    });

});